package com.ruoyi.web.controller.medicine;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MedMo;
import com.ruoyi.system.service.IMedMoService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MedicalOrder;
import com.ruoyi.system.service.IMedicalOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 处方管理Controller
 *
 * @author yangxp
 * @date 2020-08-06
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/medicine/medicalOrder")
public class MedicalOrderController extends BaseController {

	private final IMedicalOrderService iMedicalOrderService;

	private final IMedMoService medMoService;

	/**
	 * 查询处方管理列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicalOrder:list')")
	@GetMapping("/list")
	public TableDataInfo list(MedicalOrder medicalOrder) {
		startPage();
		QueryWrapper<MedicalOrder> lqw = new QueryWrapper<>();
		if (StringUtils.isNotBlank(medicalOrder.getMoId())) {
			lqw.eq("mo_id", medicalOrder.getMoId());
		}
		if (StringUtils.isNotBlank(medicalOrder.getMoName())) {
			lqw.like("mo_name", medicalOrder.getMoName());
		}
		if (StringUtils.isNotBlank(medicalOrder.getCreator())) {
			lqw.like("creator", medicalOrder.getCreator());
		}
		if (StringUtils.isNotBlank(medicalOrder.getPatName())){
			lqw.like("pat_name", medicalOrder.getPatName());
		}
		List<MedicalOrder> list = iMedicalOrderService.listMedicalOrder(lqw);
		return getDataTable(list);
	}

	/**
	 * 导出处方管理列表
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicalOrder:export')")
	@Log(title = "处方管理", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MedicalOrder medicalOrder) {
		LambdaQueryWrapper<MedicalOrder> lqw = new LambdaQueryWrapper<MedicalOrder>(medicalOrder);
		List<MedicalOrder> list = iMedicalOrderService.list(lqw);
		ExcelUtil<MedicalOrder> util = new ExcelUtil<MedicalOrder>(MedicalOrder.class);
		return util.exportExcel(list, "medicalOrder");
	}

	/**
	 * 获取处方管理详细信息
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicalOrder:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		MedicalOrder medicalOrder = iMedicalOrderService.getById(id);
		String moId = medicalOrder.getMoId();
		List<MedMo> medicineList = medMoService.list(new LambdaQueryWrapper<MedMo>().eq(MedMo::getMoId, moId));
		medicalOrder.setMedicineList(medicineList);
		return AjaxResult.success(medicalOrder);
	}

	/**
	 * 获取处方管理详细信息
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicalOrder:query')")
	@GetMapping(value = "moId/{moId}")
	public AjaxResult getInfoByMoId(@PathVariable("moId") String moId) {
		MedicalOrder medicalOrder = iMedicalOrderService.getOne(new LambdaQueryWrapper<MedicalOrder>().eq(MedicalOrder::getMoId, moId));
		return AjaxResult.success(medicalOrder);
	}

	/**
	 * 新增处方管理
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicalOrder:add')")
	@Log(title = "处方管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MedicalOrder medicalOrder) {
		return toAjax(iMedicalOrderService.saveMo(medicalOrder) ? 1 : 0);
	}

	/**
	 * 修改处方管理
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicalOrder:edit')")
	@Log(title = "处方管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MedicalOrder medicalOrder) {
		return toAjax(iMedicalOrderService.updateById(medicalOrder) ? 1 : 0);
	}

	/**
	 * 删除处方管理
	 */
	@PreAuthorize("@ss.hasPermi('medicine:medicalOrder:remove')")
	@Log(title = "处方管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(iMedicalOrderService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
	}
}
